package weibo;

import user.UserSession;
import utils.MysqlUtils;

import javax.servlet.http.HttpSession;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;

public class Weibo {
    public static ArrayList<HashMap<String, String>> getComment(String blogId, int fromInt, int toInt, HttpSession session){
        ResultSet commentSet = (new MysqlUtils()).listWeiboComment(blogId, fromInt, toInt);

        try {
            ArrayList<HashMap<String, String>> array = new ArrayList<>();
            while (commentSet.next()){
                HashMap<String, String> item = new HashMap<>();

                // interact.id,user_id,nickname,head_image,content,time
                String commentId = commentSet.getString(1);
                String userId = commentSet.getString(2);
                String nickname = commentSet.getString(3);
                String headImage = commentSet.getString(4);
                String content = commentSet.getString(5);
                String time = commentSet.getString(6);
                item.put("comment_id", commentId != null ? commentId : "");
                item.put("user_id", userId != null ? userId : "");
                item.put("nickname", nickname != null ? nickname : "");
                item.put("head_image", headImage != null ? headImage : "");
                item.put("content", content != null ? content : "");
                item.put("time", time != null ? time : "");


                if (session != null) {
                    String email = UserSession.getUserSessionEmail(session);
                    if (email != null && !email.isEmpty()){
                        // can_delete判断是否能删除
                        item.put("can_delete", (new MysqlUtils()).canDeleteComment(email, commentId != null ? commentId : "") ? "1" : "0");
                    }
                }


                array.add(item);
            }

            return array;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
